using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class CrossPromotionButton : MonoBehaviour
{
	public Button button;

	public Image appIcon;

	public Text appName;

	public RectTransform buttonRect;

	private CrossPromotionResult crossPromotionResult;

	private bool loaded;

	private WaitForSeconds wfs = new WaitForSeconds(10f);

	private WaitForEndOfFrame wfef = new WaitForEndOfFrame();

	private void Awake()
	{
		buttonRect = (RectTransform)button.transform;
		button.onClick.AddListener(delegate
		{
			StartCoroutine(CrossPromotionHandler.SendClickToServer(crossPromotionResult.appUrl, crossPromotionResult.appBundleId, 5f));
		});
	}

	private void OnEnable()
	{
		CrossPromotionHandler.OnCrossPromotionSuccess += CrossPromotionHandler_OnCrossPromotionSuccess;
		Utils.OnSessionCountChanged += Utils_OnSessionCountChanged;
		if (!loaded && CrossPromotionHandler.CrosspromoLoaded)
		{
			CrossPromotionHandler_OnCrossPromotionSuccess(CrossPromotionHandler.Result);
		}
	}

	private void OnDisable()
	{
		CrossPromotionHandler.OnCrossPromotionSuccess -= CrossPromotionHandler_OnCrossPromotionSuccess;
		Utils.OnSessionCountChanged -= Utils_OnSessionCountChanged;
	}

	private void CrossPromotionHandler_OnCrossPromotionSuccess(CrossPromotionResult result)
	{
		crossPromotionResult = result;
		if (appIcon.sprite != null && appIcon.sprite.texture != null)
		{
			Object.Destroy(appIcon.sprite.texture);
		}
		appIcon.sprite = Sprite.Create(crossPromotionResult.texture, new Rect(0f, 0f, crossPromotionResult.texture.width, crossPromotionResult.texture.height), Vector2.zero);
		appName.text = crossPromotionResult.appName;
		StartCoroutine(MoveOverSeconds(new Vector2(25f, buttonRect.anchoredPosition.y), 0.5f));
		StartCoroutine(ScaleUpDown());
		loaded = true;
	}

	private void Utils_OnSessionCountChanged()
	{
		buttonRect.anchoredPosition = new Vector2(0f - buttonRect.sizeDelta.x - 20f, buttonRect.anchoredPosition.y);
		StopAllCoroutines();
	}

	private IEnumerator ScaleUpDown()
	{
		while (true)
		{
			yield return wfs;
			yield return StartCoroutine(ScaleOverSeconds(1.1f, 0.3f));
			yield return StartCoroutine(ScaleOverSeconds(1f, 0.3f));
		}
	}

	private IEnumerator ScaleOverSeconds(float end, float seconds)
	{
		float elapsedTime = 0f;
		float startingScale = appIcon.transform.localScale.x;
		float currentScale = 0f;
		while (elapsedTime <= seconds)
		{
			currentScale = Mathf.Lerp(startingScale, end, elapsedTime / seconds);
			appIcon.transform.localScale = Vector3.one * currentScale;
			elapsedTime += Time.deltaTime;
			yield return wfef;
		}
		appIcon.transform.localScale = Vector3.one * end;
	}

	private IEnumerator MoveOverSeconds(Vector2 end, float seconds)
	{
		float elapsedTime = 0f;
		Vector2 startingPos = buttonRect.anchoredPosition;
		while (elapsedTime <= seconds)
		{
			buttonRect.anchoredPosition = Vector2.Lerp(startingPos, end, elapsedTime / seconds);
			elapsedTime += Time.deltaTime;
			yield return wfef;
		}
		buttonRect.anchoredPosition = end;
	}
}
